package i2.application.checklist.services.impl;

import i2.application.checklist.dao.ApplicationDao;
import i2.application.checklist.dao.LivrableDao;
import i2.application.checklist.entities.Application;
import i2.application.checklist.entities.Livrable;
import i2.application.checklist.services.AdministrationService;
import i2.application.fmw.aspects.Validation;
import i2.application.fmw.codes.CodeList;
import i2.application.fmw.exceptions.ValidationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;


@Transactional
public class AdministrationServiceImpl implements AdministrationService {

	private static final Logger logger = LoggerFactory.getLogger(AdministrationServiceImpl.class);

	private ApplicationDao applicationDao;

	private LivrableDao livrableDao;

	private CodeList<Application> enumApplication;

	private CodeList<Livrable> enumLivrable;

	@Validation
	@Override
	public void updateApplication(Application application) {

		if (applicationDao.isNameAlreadyUsed(application)){
			logger.warn("le nom {} est déjà utilisé", application.getNom());
			throw new ValidationException("application.nom", "application.nom.unique");
		}
		applicationDao.update(application);
		enumApplication.reset();
	}

	@Validation
	@Override
	public void createApplication(Application application) {
		if (applicationDao.isNameAlreadyUsed(application)){
			logger.warn("le nom {} est déjà utilisé", application.getNom());
			throw new ValidationException("application.nom", "application.nom.unique");
		}
		applicationDao.add(application);

		enumApplication.reset();
	}

	@Override
	public void deleteApplication(Application application) {
		// il est interdit de supprimer une application qui a des livraisons
		if (applicationDao.hasLivraisons(application)) {
			logger.warn("Action impossible : l'application {} a des livraisons", application.getNom());
			throw new ValidationException("application.nom", "application.has.livraisons");
		}

		applicationDao.delete(application);
		enumApplication.reset();
	}

	@Override
	public void setEnumApplication(CodeList<Application> enumApplication) {
		this.enumApplication = enumApplication;
	}

	@Override
	public void createLivrable(Livrable livrable) {
		livrableDao.add(livrable);
		enumLivrable.reset();

	}
	
	@Validation
	@Override
	public void updateLivrable(Livrable livrable) {
		livrableDao.update(livrable);
		enumLivrable.reset();
	}

	@Override
	public void deleteLivrable(Livrable Livrable) {
		livrableDao.delete(Livrable);
		this.enumLivrable.reset();
	}

	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}
	
	public void setLivrableDao(LivrableDao livrableDao) {
		this.livrableDao = livrableDao;
	}

	public void setEnumLivrable(CodeList<Livrable> enumLivrable) {
		this.enumLivrable = enumLivrable;
	}
	
}
